package PlaneGame.CODE;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import static PlaneGame.CODE.GameFrame.*;

public class MrW extends GameImage{

    LostString lostString=new LostString("",160,300);
    boolean NB,invalid;
    Vector<BulletA> bulletAS=new Vector<>();
    int twinkleRate=10,twinkleR;//闪烁频率
    int twinkleCount=5,twinkleC;//闪烁次数
    boolean[] directions;
    int speed;
    int explodeImageNumber;
    int blood,score,bulletNumber;

    public MrW() {
        super();
        directions= new boolean[]{false, false, false, false};
        this.width=40;
        this.height=40;
        this.x=(frameWidth/2-this.width/2);
        this.y=500;
        this.imageNumber=2;
        this.explodeImageNumber=4;
        this.paths= new String[]{"src/PlaneGame/IMAGE/me1.png",
                "src/PlaneGame/IMAGE/me2.png",
                "src/PlaneGame/IMAGE/me_destroy_1.png",
                "src/PlaneGame/IMAGE/me_destroy_2.png",
                "src/PlaneGame/IMAGE/me_destroy_3.png",
                "src/PlaneGame/IMAGE/me_destroy_4.png"};
        images=new BufferedImage[imageNumber+explodeImageNumber];
        for (int i = 0; i < images.length; i++) {
            try {
                images[i] = ImageIO.read(new File(paths[i]));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    void init() {
        super.init();
        NB=false;
        invalid=false;
        blood=3;
        bulletNumber=100;
        speed=8;
        score=0;
        twinkleR=0;
        twinkleC=0;
    }

    @Override
    void nextDrawPos() {
        if(blood>0){
            super.nextDrawPos();
        }else {
            if(drawPos<imageNumber+explodeImageNumber-1){
                drawPos++;
            }else {
                off();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                gameData.xie(score);
                recordString.show();
                lostString.show();
                suspendButton.suspend();
            }
        }
    }
    @Override
    void change() {
        super.change();
        move();
    }
    void stop(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_UP){
            directions[0]=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            directions[1]=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            directions[2]=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            directions[3]=false;
        }
    }
    void turn(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_UP){
            directions[0]=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            directions[1]=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            directions[2]=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            directions[3]=true;
        }
    }
    void move(){
        if (blood>0) {
            if(directions[0]){
                if (y>0) {
                    y-=speed;
                }
            }
            if(directions[1]){
                if (y+2*height<frameHeight) {
                    y+=speed;
                }
            }
            if(directions[2]){
                if (x+width*0.3>0) {
                    x-=speed;
                }
            }
            if(directions[3]){
                if (x+width<frameWidth) {
                    x+=speed;
                }
            }
        }
    }
    void OhGan(){
        NB=true;
    }
    void OhNo(){
        blood--;
        if (blood>0) {
            invalid=true;
        }
    }
    void OhYeah(){
        blood++;
    }
    void OhDamn(){
        bulletNumber+=100;
    }
    void twinkle(Graphics g){
        if (twinkleR>twinkleRate) {
            twinkleR=-twinkleRate;
            if(twinkleC<twinkleCount){
                twinkleC++;
            }else {
                twinkleC=0;
                invalid=false;
            }
        }else {
            twinkleR++;
        }
        if(twinkleR>0){
            super.draw(g);
        }
    }
    @Override
    void draw(Graphics g) {
        if (invalid) {
            twinkle(g);
            drawVector(g);
        }else {
            super.draw(g);
            drawVector(g);
        }
    }
    void shot(KeyEvent e){
        if (on_off) {
            if(e.getKeyCode()==KeyEvent.VK_SPACE){
                delete();
                create();
            }
        }
    }
    void create(){
        if (bulletNumber>0) {
            bulletAS.add(new BulletA((this.x+this.width/2),this.y));
            bulletAS.lastElement().shift();
            new Thread(bulletAS.lastElement()).start();
            bulletNumber--;
        }
    }
    void delete(){
        for (int i = 0; i < bulletAS.size(); i++) {
            if(!bulletAS.elementAt(i).on_off){
                bulletAS.removeElementAt(i);
            }
        }
    }
    void drawVector(Graphics g){
        for (int i = 0; i < bulletAS.size(); i++) {
            bulletAS.elementAt(i).drawMyself(g);
        }
    }
}
